/**
 * @file models/accountDailies.js
 * 分析报告
 * @author FDTAI
 */

import { routerRedux } from 'dva/router';
import _ from 'lodash';
import api from '../api';
import { delay } from '../utils/sagaEffects';

export default {
	namespace: 'accountAssets',
	state: {
		openPositionList: [],
		accountDaily: undefined,
		specialTradings: [],
		educationDocs: undefined,
		commentConfig: {},
		featureRanks: [],
	},
	reducers: {
		getAccountDailySuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
				accountDaily: response.data,
			};
		},
		getOpenPositionListSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
				openPositionList: response,
			};
		},
		getSpecialTradingsSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
				specialTradings: response,
			};
		},
		getCommentConfigSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
				commentConfig: response,
			};
		},
		getFeatureRanksSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
				featureRanks: response,
			};
		},
		getEducationDocsSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
				educationDocs: response,
			};
		},
	},
	effects: {
		* getAccountDaily({ payload: query }, { call, put }) {
			const response = yield call(api.getAccountDailies, query);
			yield put({
				type: 'getAccountDailySuccess',
				payload: { response },
			});
		},
		* getOpenPositionList({ payload: query }, { call, put }) {
			const response = yield call(api.getOpenPositions, query);
			yield put({
				type: 'getOpenPositionListSuccess',
				payload: { response },
			});
		},
		* getSpecialTradings({ payload: query }, { call, put }) {
			const response = yield call(api.getSpecialTradings, query);
			const commentCalls = [];
			_(response).forEach(item => {
				commentCalls.push(call(api.getCommentClosedPos, {
					trader: item.trader,
					openOrderId: item.openOrderId,
					closeOrderId: item.closeOrderId,
				}));
			});
      try {
        const comments = yield commentCalls;
        _(response).forEach((item, index) => {
          item.closeOrder.comment = comments[index].data;
        });
      } catch (e) {
        _(response).forEach((item, index) => {
          item.closeOrder.comment = {};
        });
      }
			yield put({
				type: 'getSpecialTradingsSuccess',
				payload: { response },
			});
		},
		* getCommentConfig({ payload: query }, { call, put }) {
			const response = yield call(api.getCommentConfig, query);
			yield put({
				type: 'getCommentConfigSuccess',
				payload: { response },
			});
		},
		* getFeatureRanks({ payload: query }, { call, put }) {
			const response = yield call(api.getFeatureRanks, query);
			yield put({
				type: 'getFeatureRanksSuccess',
				payload: { response },
			});
		},
		* getEducationDocs({ payload: query }, { call, put }) {
			const response = yield call(api.getEducationDocs, query);
			yield put({
				type: 'getEducationDocsSuccess',
				payload: { response },
			});
		},
	},
	subscriptions: {},
};
